﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class GoodsClass
    {
        public int Add(Chain.Model.GoodsClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsClass(");
            builder.Append("ClassName,ClassRemark,ParentID,CreateShopID)");
            builder.Append(" values (");
            builder.Append("@ClassName,@ClassRemark,@ParentID,@CreateShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.VarChar, 50), new SqlParameter("@ClassRemark", SqlDbType.NVarChar, 100), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@CreateShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ClassName;
            cmdParms[1].Value = model.ClassRemark;
            cmdParms[2].Value = model.ParentID;
            cmdParms[3].Value = model.CreateShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsClass ");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int DeleteByParentID(int ParentID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsClass ");
            builder.Append(" where ParentID=@ParentID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ParentID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ParentID;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return -3;
        }

        public bool DeleteList(string ClassIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsClass ");
            builder.Append(" where ClassID in (" + ClassIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsClass");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string ClassName, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsClass");
            builder.Append(" where ClassName=@ClassName");
            builder.Append(" and ShopID=@ShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.VarChar, 50), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassName;
            cmdParms[1].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int classID, string ClassName, int CreateShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsClass");
            builder.Append(" where ClassID not in (@ClassID) ");
            builder.Append(" and (ClassName=@ClassName)");
            builder.Append(" and CreateShopID=@CreateShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int), new SqlParameter("@ClassName", SqlDbType.VarChar, 50), new SqlParameter("@CreateShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = classID;
            cmdParms[1].Value = ClassName;
            cmdParms[2].Value = CreateShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetItem(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ClassID,ClassName,ParentID,ClassRemark,ShopID from GoodsClass ");
            builder.Append(" where ClassID=@ClassID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ClassID,ClassName,ClassRemark,ParentID,CreateShopID ");
            builder.Append(" FROM GoodsClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ClassID,ClassName,ClassRemark,ParentID,CreateShopID ");
            builder.Append(" FROM GoodsClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ClassID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsClass T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByShopID(int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GoodsClass.ClassID,ClassName,ClassRemark,ParentID,GoodsClassAuthority.ShopID ");
            builder.Append(" FROM GoodsClass INNER JOIN GoodsClassAuthority ");
            builder.Append(" ON GoodsClass.ClassID = GoodsClassAuthority.ClassID ");
            if (ShopID > 0)
            {
                builder.Append(" AND GoodsClassAuthority.ShopID = " + ShopID);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ClassID", "GoodsClass");
        }

        public Chain.Model.GoodsClass GetModel(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ClassID,ClassName,ClassRemark,ParentID,CreateShopID from GoodsClass ");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            Chain.Model.GoodsClass class2 = new Chain.Model.GoodsClass();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ClassID"] != null) && (set.Tables[0].Rows[0]["ClassID"].ToString() != ""))
            {
                class2.ClassID = int.Parse(set.Tables[0].Rows[0]["ClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ClassName"] != null) && (set.Tables[0].Rows[0]["ClassName"].ToString() != ""))
            {
                class2.ClassName = set.Tables[0].Rows[0]["ClassName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ClassRemark"] != null) && (set.Tables[0].Rows[0]["ClassRemark"].ToString() != ""))
            {
                class2.ClassRemark = set.Tables[0].Rows[0]["ClassRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ParentID"] != null) && (set.Tables[0].Rows[0]["ParentID"].ToString() != ""))
            {
                class2.ParentID = int.Parse(set.Tables[0].Rows[0]["ParentID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CreateShopID"] != null) && (set.Tables[0].Rows[0]["CreateShopID"].ToString() != ""))
            {
                class2.CreateShopID = int.Parse(set.Tables[0].Rows[0]["CreateShopID"].ToString());
            }
            return class2;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.GoodsClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsClass set ");
            builder.Append("ClassName=@ClassName,");
            builder.Append("ClassRemark=@ClassRemark,");
            builder.Append("ParentID=@ParentID,");
            builder.Append("CreateShopID=@CreateShopID");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.VarChar, 50), new SqlParameter("@ClassRemark", SqlDbType.NVarChar, 100), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@CreateShopID", SqlDbType.Int, 4), new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ClassName;
            cmdParms[1].Value = model.ClassRemark;
            cmdParms[2].Value = model.ParentID;
            cmdParms[3].Value = model.CreateShopID;
            cmdParms[4].Value = model.ClassID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

